<?php
/**
 * Created by PhpStorm.
 * User: CuiLiwu
 * Date: 2018/4/19
 * Time: 15:23
 */

namespace App\Http\Controllers\Web\Member\Seller;

use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;

class ShopBannersController extends BaseController
{
    protected $api_url = [
        // 请求列表
        'index' => ['get', 'seller/banners'],
        // 单条创建
        'store' => ['post', 'seller/banners'],
        // 单条更新
        'update' => ['put', 'seller/banners/%d'],
        // 单条删除
        'delete' => ['delete', 'seller/banners/%d'],
    ];

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * 列表页
     * method get
     * url /
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        //请求部分
        $ret  = http_web($this->api_url['index'])->send();
        $data = paginate($this->request, $ret['data']);
        foreach ($data['list'] as $key=>$value){
            if ($value['screen_type']==1){
                $data['first'] = $value;
            }elseif($value['screen_type']==2){
                $data['second'] = $value;
            }else{
                $data['third'] = $value;
            }
        }
        return view('web.user.seller.shop_design_banner',$data);
    }

    /**
     * 详情页
     * method get
     * url /$id
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show($id)
    {
        // 无详情页
    }

    /**
     * 创建页
     * method get
     * url /create
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
    }

    /**
     * 创建-form提交
     * method post
     * url /
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function store()
    {
        //验证部分
        $data = $this->form();

        //请求部分
        $res = http_web($this->api_url['store'])->send($data);
        return $res;
    }

    /**
     * 更新页
     * method get
     * url /$id/edit
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id)
    {
    }

    /**
     * 更新-form提交
     * method put
     * url /$id
     * @param $id
     */
    public function update($id)
    {
        //验证部分
        $data = $this->form();
        //请求部分
        $ret = http_web($this->api_url['update'], [$id])->send($data);
        //结果处理部分
        return $ret;
    }
    /**
     * 删除-ajax提交
     * method delete
     * url /$id
     * @param $id
     */
    public function destroy($id)
    {
        //验证部分
        $this->validate($this->request, [
            'screen_type' => 'required|in:1,2,3',
        ],[
            'screen_type' => 'screen_type必须为数字|type必须为1或2或3',
        ]);
        //请求部分
        $ret = http_web($this->api_url['delete'], [$id])->send();

        //结果处理部分
        return $ret;
    }

    /**
     * 创建、更新公共验证部分
     * @return array
     */
    private function form()
    {
        $this->validate($this->request, [
            'banner_name' => 'required',
            'banner_url'  => $this->request->banner_url!=null ?'sometimes|regex:/ejing365.com/':'',
            'banner_image'=> 'required',
            'screen_type' => 'required|in:1,2,3',
        ],[
            'banner_name' => 'banner标题必填',
            'banner_url'  => '仅支持内部链接，即以ejing365.com为主域名的链接',
            'banner_image'=> '必须选择展示的图片',
            'screen_type' => 'screen_type必须为数字|type必须为1或2或3',
        ]);

        return $this->request->all(['banner_name', 'banner_image','screen_type','banner_url']);
    }
}